package tables

import (
	"fmt"

	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/icon"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/action"
	"github.com/GoAdminGroup/go-admin/template/types/form"
	editType "github.com/GoAdminGroup/go-admin/template/types/table"
)

func GetAymodelTable(ctx *context.Context) table.Table {

	ayModel := table.NewDefaultTable(ctx, table.DefaultConfigWithDriverAndConnection("sqlite", "pbootcms"))
	tblName := "ay_model"
	info := ayModel.GetInfo()
	result, _ := db.WithDriverAndConnection("pbootcms", conn).
		Table(tblName).Select("count(mcode)").First()
	maxcode := fmt.Sprint(result["count(`mcode`)"])
	fmt.Printf("%+v", result)
	// map[count(`mcode`):6]
	info.AddField("序号", "id", db.Integer).FieldSortable()
	info.AddField("模型编号", "mcode", db.Text)
	info.AddField("模型名称", "name", db.Text).
		FieldFilterable(types.FilterType{Operator: types.FilterOperatorLike}).
		FieldSortable().
		FieldEditAble()
	info.AddField("是否列表类型", "type", db.Text).FieldDisplay(func(model types.FieldModel) interface{} {
		if model.Value == "2" {
			return "是"
		}
		if model.Value == "1" {
			return "否"
		}
		return "unknown"
	}).FieldEditAble(editType.Switch).FieldEditOptions(types.FieldOptions{
		{Value: "2", Text: "是"},
		{Value: "1", Text: "否"},
	})
	info.AddField("URL名称", "urlname", db.Text)
	info.AddField("列表页模板", "listtpl", db.Text).FieldEditAble()
	info.AddField("内容页模板", "contenttpl", db.Text).FieldEditAble()
	info.AddField("模型状态", "status", db.Text).FieldDisplay(filter01("隐藏", "显示")).
		FieldEditAble(editType.Switch).FieldEditOptions(opt01("隐藏", "显示"))
	info.AddField("系统模型", "issystem", db.Text).FieldDisplay(filter01("否", "是")).
		FieldEditAble(editType.Switch).FieldEditOptions(opt01("否", "是"))
	info.AddSelectBox(ctx, "issystem", opt01("否", "是"), action.FieldFilter("issystem"))

	info.AddField("创建人员", "create_user", db.Text).FieldEditAble()
	info.AddField("更新人员", "update_user", db.Text).FieldEditAble()
	info.AddField("创建时间", "create_time", db.Text)
	info.AddField("更新时间", "update_time", db.Text)
	info.AddButton(ctx, "分类列表", icon.Tv, action.PopUpWithIframe("/ay_content_sort/list",
		"文章分类", action.IframeData{Src: "/admin/info/ay_content_sort"}, "900px", "560px"))

	info.SetTable("ay_model").SetTitle("模型").SetDescription("模型")

	formList := ayModel.GetForm()
	formList.AddField("序号", "id", db.Integer, form.Default).FieldDisplayButCanNotEditWhenUpdate().FieldNotAllowAdd()
	formList.AddField("模型编号", "mcode", db.Text, form.Number).FieldDefault(maxcode)
	formList.AddField("模型名称", "name", db.Text, form.Text)
	formList.AddField("是否列表类型", "type", db.Text, form.Radio).
		FieldOptions(types.FieldOptions{
			{Text: "是", Value: "2"},
			{Text: "否", Value: "1"},
		}).FieldDefault("2")
	formList.AddField("URL", "urlname", db.Text, form.Text).FieldDefault("list")
	formList.AddField("列表页模板", "listtpl", db.Text, form.Text).FieldDefault("newslist.html")
	formList.AddField("内容页模板", "contenttpl", db.Text, form.Text).FieldDefault("news.html")

	formList.AddField("模型状态", "status", db.Text, form.Radio).
		FieldOptions(opt01("隐藏", "显示")).FieldDefault("1")
	formList.AddField("是否系统模型", "issystem", db.Text, form.Radio).
		FieldOptions(opt01("否", "是")).FieldDefault("0")
	formList.AddField("创建人员", "create_user", db.Text, form.Text).FieldDefault("admin")
	formList.AddField("更新人员", "update_user", db.Text, form.Text).FieldDefault("admin")
	formList.AddField("创建时间", "create_time", db.Text, form.Datetime).FieldNowWhenInsert()
	formList.AddField("更新时间", "update_time", db.Text, form.Datetime).FieldNowWhenUpdate()
	ayModel.GetForm().SetTabGroups(types.
		NewTabGroups("id", "mcode", "name", "type", "urlname", "listtpl",
			"contenttpl", "status", "issystem").
		AddGroup("create_user", "update_user", "create_time", "update_time")).
		SetTabHeaders("基本信息", "更多信息")
	formList.SetTable("ay_model").SetTitle("编辑模型").SetDescription("编辑模型")

	return ayModel
}
